﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Nginx正向代理</title>
    <script type="text/javascript" src="../Content/highlighter/scripts/shCore.js"></script>
    <script type="text/javascript" src="../Content/highlighter/scripts/shBrushCSharp.js"></script>
    <script type="text/javascript" src="../Content/highlighter/scripts/shBrushXml.js"></script>
    <link type="text/css" rel="stylesheet" href="../Content/highlighter/styles/shCoreDefault.css" />
    <script type="text/javascript">SyntaxHighlighter.all();</script>
</head>
<body>
    <h3>正向代理与反向代理的区分</h3>
    <p>
        正向代理指代理服务器请求头中带有客户端的IP,反向代理则是不带IP.
        1.正向代理是指客户端的代理,其代理服务器能拿到客户端的数据,比如IP,User-Agent,操作系统等信息,而最终的服务器则是拿不到这些信息的.
        由于路由等拿不到原始用户的数据,一般用于翻墙,玩游戏等.
        2.反向代理是指服务端的代理,其服务器必须配置后端响应服务器的IP以及端口,此服务器会把用户的请求分发给不同的服务器对应的IP及端口.
        代理服务器把请求发给不同的服务器,其对请求内容并不作任何处理,所以一般用于负载均衡.
    </p>
    <h3>正向代理的配置</h3>
    <pre>
    server{  
            resolver 8.8.8.8;  
            resolver_timeout 30s;   
            listen 82;  
            location / {  
                    proxy_pass http://$http_host$request_uri;  
                    proxy_set_header Host $http_host;  
                    proxy_buffers 256 4k;  
                    proxy_max_temp_file_size 0;  
                    proxy_connect_timeout 30;  
                    proxy_cache_valid 200 302 10m;  
                    proxy_cache_valid 301 1h;  
                    proxy_cache_valid any 1m;  
            }  
    }  
    </pre>
    <h3>反向代理配置</h3>
    <pre>
        http {  
        #   省略了前面一般的配置，直接从负载均衡这里开始  
        #   设置地址池，后端3台服务器  
            upstream http_server_pool {  
                server 192.168.1.2:8080 weight=2 max_fails=2 fail_timeout=30s;  
                server 192.168.1.3:8080 weight=3 max_fails=2 fail_timeout=30s;  
                server 192.168.1.4:8080 weight=4 max_fails=2 fail_timeout=30s;  
            }  
        #   一个虚拟主机，用来反向代理http_server_pool这组服务器  
            server {  
                listen       80;  
        #       外网访问的域名          
                server_name  www.test.com;   
                location / {  
        #           后端服务器返回500 503 404错误，自动请求转发到upstream池中另一台服务器  
                    proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;  
                    proxy_pass http://http_server_pool;  
                    proxy_set_header Host www.test.com;  
                    proxy_set_header X-Real-IP $remote_addr;  
                    proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;  
                }  
                access_log  logs/www.test.com.access.log  combined;  
            }  
        }  
    </pre>
    <pre>
        server {  
            listen    80;  
            server_name test.zhoumengkang.com;  
            location / {  
                proxy_pass http://121.199.**.*:80;  
            }  
        }  
    </pre>
</body>
</html>